package miku.firstgame.entity
{
	import com.saia.starlingPunk.SP;
	import com.saia.starlingPunk.SPEntity;
	import com.saia.starlingPunk.SPMask;
	import com.saia.starlingPunk.graphics.SPTile;
	import com.saia.starlingPunk.utils.Key;
	import com.saia.starlingPunk.utils.SPInput;

	import flash.geom.Point;
	import flash.media.Video;

	import flashx.textLayout.elements.OverflowPolicy;

	import miku.firstgame.event.PlayerEvent;

	import starling.display.Image;
	import starling.textures.Texture;

	public class TestPlayer extends SPEntity
	{
		private const ACEL:Number = 1;
		public static const CELL_SIZE:Number = 32;

		[Embed(source = "media/burger.png")]
		public const ui:Class;

		public var velocityX:Number = 0;
		public var velocityY:Number = 0;

		public var maxSpeed:Number = 2;

		public function TestPlayer()
		{
			super(32, 32, "player");
		}

		override public function added():void
		{
			super.added();
			setupGraphic();
		}

		private function setupGraphic():void
		{
			//var textureAtlas:TextureAtlas = new TextureAtlas(Texture.fromBitmap(new ExampleAssets.ATLAS_TEXTURE()), XML(new ExampleAssets.ATLAS_DATA()));
			var image:Image = new Image(Texture.fromBitmap(new ui())); //textureAtlas.getTexture("burger"));
			addChild(image);
		}

		override public function removed():void
		{
			removeChildren(0, -1, true);
			super.removed();
		}

		override public function update():void
		{
			super.update();
			updateMovement();
			updateInput();
			updateCollision();

			//SP.passedTime 数据同步依据
		}

		private function updateMovement():void
		{
			this.x += velocityX;
			this.y += velocityY;
		}

		private function updateInput():void
		{

//			var isLeft:Boolean = false;
//			var isUp:Boolean = false;

			if (SPInput.check(Key.LEFT))
			{
				//isLeft = true;
				//trace("Key.LEFT");
				if (Math.abs(velocityX) < maxSpeed)
				{
					velocityX -= ACEL;
						//clearVY();
				}

				velocityX = -Math.abs(velocityX);
			}
//			else
//			{ 
//				isLeft = false;
//			}
			if (SPInput.check(Key.RIGHT))
			{
				//trace("Key.RIGHT");

//				if (isLeft)
//				{
//					clearVY(); 
//					clearVX()
//				}
//				else 
//				{
				if (Math.abs(velocityX) < maxSpeed)
				{
					velocityX += ACEL;
						//clearVY();
				}
				velocityX = Math.abs(velocityX);
					//}
			}
			if (SPInput.check(Key.UP))
			{
				//trace("Key.UP");
				if (Math.abs(velocityY) < maxSpeed)
				{
					velocityY -= ACEL;
						//clearVX(); 
				}
				velocityY = -Math.abs(velocityY);
			}
			if (SPInput.check(Key.DOWN))
			{
				//trace("Key.DOWN");
				if (Math.abs(velocityY) < maxSpeed)
				{
					velocityY += ACEL;
						//clearVX();
				}
				velocityY = Math.abs(velocityY);
			}

			if (!SPInput.check(Key.LEFT) && !SPInput.check(Key.DOWN) && !SPInput.check(Key.RIGHT) && !SPInput.check(Key.UP))
			{
				clearVX();
				clearVY();

					//trace("velocity clear"); 
			}

			if (x + velocityX <= 0 || x + velocityX >= 32 * 9)
			{
				clearVX();
			}

			if (y + velocityY <= 0 || y + velocityY >= 32 * 9)
			{
				clearVY();
			}

			if (collide("collision", x + velocityX, y + velocityY) || collide("glassCollision", x + velocityX, y + velocityY))
			{
				clearVX();
				clearVY();
			}

			var collideObject:SPEntity = collide("testTank", x + velocityX, y + velocityY) as SPEntity;
			if (collideObject)
			{
				clearVX();
				clearVY();
			}

//			if (collide("glassLayer", x + velocityX, y + velocityY))
//			{
//				trace("collision width glassLayer");
//			}

			if (SPInput.pressed(Key.SPACE))
			{
				dispatchBall();
			}

//			if (SPInput.check("jump") && this.collide("collision", x, y + 1) && _isJumpReleased)
//			{
//				_vel.y = -JUMP_SPEED;
//				_isJumpReleased = false;
//			}
//
//			if (SPInput.released("jump"))
//			{
//				_isJumpReleased = true;
//			}
		}

		//抛事件或者
		private function dispatchBall():void
		{
			var event:PlayerEvent = new PlayerEvent(PlayerEvent.PLACE_BOMB);
			dispatchEvent(event);
		}

		private function clearVX():void
		{

			var stopX:Number;
			if (velocityX > 0)
			{
				stopX = Math.ceil(x / 32) * 32;
			}
			else if (velocityX < 0)
			{
				stopX = Math.floor(x / 32) * 32;
			}
			else
			{
				velocityX = 0;
				return
			}
			if (stopX == x)
			{
				velocityX = 0
			}
			else
			{
				var distance:Number = stopX - x;

				if (Math.abs(distance) > maxSpeed)
				{
					velocityX = Math.abs(distance) / distance * maxSpeed;
				}
				else
				{
					velocityX = Math.abs(distance) / distance;
				}
			}

		}

		private function clearVY():void
		{
			var stopY:Number;
			if (velocityY > 0)
			{
				stopY = Math.ceil(y / 32) * 32;
			}
			else if (velocityY < 0)
			{
				stopY = Math.floor(y / 32) * 32;
			}
			else
			{
				velocityY = 0;
				return
			}
			if (stopY == y)
			{
				velocityY = 0
			}
			else
			{
				var distance:Number = stopY - y;

				if (Math.abs(distance) > maxSpeed)
				{
					velocityY = Math.abs(distance) / distance * maxSpeed;
				}
				else
				{
					velocityY = Math.abs(distance) / distance;
				}
			}
		}

		private function updateCollision():void
		{

		}

	}
}
